package com.sgb.kjwl.view.ui.activity.self.publish;

import android.annotation.SuppressLint;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.GsonManager;
import com.sgb.kjwl.databinding.ActivityAddGoodsBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.AddGoodsItemEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.sgb.kjwl.model.entity.SuccessEntity;
import com.sgb.kjwl.model.entity.user.AddGoodsEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.NewCategeryEntity;
import com.sgb.kjwl.model.entity.user.UnitEntity;
import com.sgb.kjwl.view.event.SuccessType;
import com.sgb.kjwl.view.ui.activity.self.authentication.CommitSuccessActivity;
import com.sgb.kjwl.view.ui.adapter.self.publish.ImageAdapter;
import com.sgb.kjwl.view.widget.CategoryPopWindow;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.view.widget.SelecteUnitPopWindow;
import com.sgb.kjwl.viewmodel.self.publish.AddGoodsModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


public class AddGoodsActivity extends AppActivity {
    private ActivityAddGoodsBinding binding;
    private CategoryPopWindow categoryPopWindow;
    private List<MenuEntity<MenuEntity<MenuEntity>>> menuEntityList;

    private SelecteUnitPopWindow selecteUnitPopWindow;
    private List<LocalMedia> selectList = new ArrayList<>();
    private int currentPop;
    private ImageAdapter adapter;

    private List<String> urls;
    private List<File> fileList;
    private SuccessEntity successEntity;
    private List<UnitEntity> unitEntityList;
    private int unitPosition;

    public int imgTotal;
    private int currentImgNum;
    private List<String> uploadImgList = new ArrayList<>();
    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2;
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3;
    //城市列表
    private List<AddressEntity<List<AddressEntity>>> cityList;
    //区域列表
    private List<List<AddressEntity>> areaList;
    private boolean isLoaded = false;
    private boolean isCategeryLoaded = false;
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private int categeryPosition1;
    private int categeryPosition2;
    private int categeryPosition3;

    // 分类一级联动数据
    private List<NewCategeryEntity<List<NewCategeryEntity<List<NewCategeryEntity>>>>> categeryEntityList;
    // 分类二级联动数据
    private List<List<NewCategeryEntity<List<NewCategeryEntity>>>> categery2;
    // 分类三级联动数据
    private List<List<List<NewCategeryEntity>>> categery3;
    //分类2
    private List<NewCategeryEntity<List<NewCategeryEntity>>> cg2;
    //分类3
    private List<List<NewCategeryEntity>> cg3;

    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private static final int MSG_LOAD_FAILED = 0x0003;

    private static final int MSG_CATEGERY_LOAD_SUCCESS = 0x0005;
    private static final int MSG_CATEGERY_LOAD_DATA = 0x0006;
    @Inject
    AddGoodsModel viewModel;
    //测试数据
    private ArrayList<AddGoodsItemEntity> addGoodsItemEntities;
    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(AddGoodsActivity.this, R.layout.activity_add_goods);
        binding.include.backView.setOnClickListener(this);
        binding.include.rightView.setVisibility(View.GONE);
        binding.include.titleView.setText("发布商品");

        categoryPopWindow = new CategoryPopWindow(AddGoodsActivity.this);

        binding.llCategory.setOnClickListener(this);
        binding.llUnit.setOnClickListener(this);
        binding.llStorage.setOnClickListener(this);
        binding.tvPublish.setOnClickListener(this);

        binding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.tvTitle.setText(charSequence.toString() + binding.etBrand.getText());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        binding.etBrand.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                binding.tvTitle.setText(binding.etName.getText() + charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        //binding.llPic.setOnClickListener(this);
        adapter = new ImageAdapter(this, null, null, 5);

        binding.goodsRecyclerView.setLayoutManager(new GridLayoutManager(AddGoodsActivity.this, 4));
        binding.goodsRecyclerView.setAdapter(adapter);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void init() {
        opt2 = new ArrayList<>();
        opt3 = new ArrayList<>();
        categery2 = new ArrayList<>();
        categery3 = new ArrayList<>();
        fileList = new ArrayList<>();
        urls = new ArrayList<>();
        id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            viewModel.getGoodsById(id);
        }else {
            String addGoods = SharedPreferenceUtil.getInstance().getSaveStringData("ADD_GOODS", "");
            if (!TextUtils.isEmpty(addGoods)){
                AddGoodsEntity addGoodsEntity = GsonManager.getInstance().getGson().fromJson(addGoods, AddGoodsEntity.class);
                showEditeInfo(addGoodsEntity);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                //todo 新功能，保存编辑中内容，下一版上
                /*if(TextUtils.isEmpty(id)){
                    checkContent();
                }*/
                finish();
                break;
            case R.id.ll_category:
                if (categeryEntityList != null) {
                    showCategreyPop();
                } else {
                    viewModel.getCategeryModel();
                }
                break;
            case R.id.ll_unit:
                if (unitEntityList != null) {
                    showUnitPop();
                } else {
                    viewModel.getPriceUnitModel();
                }
                break;
            case R.id.ll_storage:
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    viewModel.getAddressModel();
                }
                break;
            case R.id.ll_pic:
                break;
            case R.id.tv_publish:
                checkPublish();
                break;
        }
    }

    private void checkContent() {
        if (binding.tvCategory.getText().toString().equals("请选择分类") && TextUtils.isEmpty(binding.etName.getText()) && TextUtils.isEmpty(binding.etBrand.getText()) && TextUtils.isEmpty(binding.etPrice.getText())
        && binding.tvUnit.getText().toString().equals("请选择单位") && TextUtils.isEmpty(binding.etRate.getText()) && binding.tvStorage.getText().toString().equals("请选择仓储")){
            finish();
        }else {
            showSaveDialog();
        }
    }

    private void checkPublish() {
        if (binding.tvCategory.getText().toString().equals("请选择分类")) {
            MToast.showToast(this, "请选择商品分类");
            return;
        }

        if (TextUtils.isEmpty(binding.etName.getText())) {
            MToast.showToast(this, "请输入商品名称");
            return;
        }

        if (TextUtils.isEmpty(binding.etBrand.getText())) {
            MToast.showToast(this, "请输入商品品牌");
            return;
        }

        if (TextUtils.isEmpty(binding.etPrice.getText())) {
            MToast.showToast(this, "请输入商品单价");
            return;
        }

        if (binding.tvUnit.getText().toString().equals("请选择单位")) {
            MToast.showToast(this, "请选择单位");
            return;
        }

        if (TextUtils.isEmpty(binding.etRate.getText())) {
            MToast.showToast(this, "请输入税率");
            return;
        }

        if (binding.tvStorage.getText().toString().equals("请选择仓储")) {
            MToast.showToast(this, "请选择仓储");
            return;
        }

        if (TextUtils.isEmpty(id)){
            if (selectList.size()==0){
                MToast.showToast(this,"请选择商品图片");
                return;
            }
        }else {
            if (adapter.getUrls().size()==0 && selectList.size()==0){
                MToast.showToast(this,"请选择商品图片");
                return;
            }
        }

        viewModel.addGoodsEntity.get().setPrice(binding.etPrice.getText().toString());
        viewModel.addGoodsEntity.get().setBrand(binding.etBrand.getText().toString());
        viewModel.addGoodsEntity.get().setName(binding.etName.getText().toString());
        viewModel.addGoodsEntity.get().setRate(binding.etRate.getText().toString());
        viewModel.addGoodsEntity.get().setTitle(binding.tvTitle.getText().toString());

        if (adapter.getFileList().size() != 0) {
            imgTotal = adapter.getFileList().size();
            viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum+1);
            //图片同步上传注释
           // viewModel.uploadMutiFileModel(adapter.getFileList(), 0);
        } else {
            uploadImgSuccess(adapter.getUrls(), 1);
        }
    }

    //点击编辑展示内容
    public void showEditeInfo(AddGoodsEntity addGoodsEntity) {
        if (!TextUtils.isEmpty(addGoodsEntity.getFirstCategoryName())){
            binding.tvCategory.setText(addGoodsEntity.getFirstCategoryName() + "/" + addGoodsEntity.getSecondCategoryName() + "/" + addGoodsEntity.getThirdCategoryName());
        }
        if (!TextUtils.isEmpty(addGoodsEntity.getProvinceName())){
            binding.tvStorage.setText(addGoodsEntity.getProvinceName() + " " + addGoodsEntity.getCityName() + " " + addGoodsEntity.getDistrictName());
        }

        binding.tvTitle.setText(addGoodsEntity.getTitle());
        binding.etName.setText(addGoodsEntity.getName());
        binding.etBrand.setText(addGoodsEntity.getBrand());
        binding.etPrice.setText(addGoodsEntity.getPrice());
        binding.tvUnit.setText(addGoodsEntity.getPriceUnitName());
        binding.etRate.setText(addGoodsEntity.getRate());

        if (addGoodsEntity.getPictureList()!=null){
            adapter.setNetImgList(addGoodsEntity.getPictureList());
        }

        viewModel.addGoodsEntity.get().setId(addGoodsEntity.getId());
        viewModel.addGoodsEntity.get().setPictureList(addGoodsEntity.getPictureList());
        viewModel.addGoodsEntity.get().setFirstCategoryName(addGoodsEntity.getFirstCategoryName());
        viewModel.addGoodsEntity.get().setSecondCategoryName(addGoodsEntity.getSecondCategoryName());
        viewModel.addGoodsEntity.get().setThirdCategoryName(addGoodsEntity.getThirdCategoryName());
        viewModel.addGoodsEntity.get().setFirstCategory(addGoodsEntity.getFirstCategory());
        viewModel.addGoodsEntity.get().setSecondCategory(addGoodsEntity.getSecondCategory());
        viewModel.addGoodsEntity.get().setThirdCategory(addGoodsEntity.getThirdCategory());
        viewModel.addGoodsEntity.get().setTitle(addGoodsEntity.getTitle());
        viewModel.addGoodsEntity.get().setName(addGoodsEntity.getName());
        viewModel.addGoodsEntity.get().setBrand(addGoodsEntity.getBrand());
        viewModel.addGoodsEntity.get().setPrice(addGoodsEntity.getPrice());
        viewModel.addGoodsEntity.get().setPriceUnitName(addGoodsEntity.getPriceUnitName());
        viewModel.addGoodsEntity.get().setPriceUnitNo(addGoodsEntity.getPriceUnitNo());
        viewModel.addGoodsEntity.get().setRate(addGoodsEntity.getRate());
        viewModel.addGoodsEntity.get().setProvinceName(addGoodsEntity.getProvinceName());
        viewModel.addGoodsEntity.get().setProvinceNo(addGoodsEntity.getProvinceNo());
        viewModel.addGoodsEntity.get().setCityName(addGoodsEntity.getCityName());
        viewModel.addGoodsEntity.get().setCityNo(addGoodsEntity.getCityNo());
        viewModel.addGoodsEntity.get().setDistrictName(addGoodsEntity.getDistrictName());
        viewModel.addGoodsEntity.get().setDistrictNo(addGoodsEntity.getDistrictNo());
    }

    //图片单张上传
    public void uploadRefresh(String url, int type){
        uploadImgList.add(url);
        if (currentImgNum+1 == imgTotal){
            uploadImgSuccess(uploadImgList,type);
        }else {
            currentImgNum++;
            viewModel.uploadMutiFileModel(adapter.getFileList().get(currentImgNum), 0, currentImgNum+1);
        }
    }

    //图片上传成功回调
    public void uploadImgSuccess(List<String> urls, int type) {
        //拼接图片url
        //String urlStr = "";
        ArrayList<String> urlList = new ArrayList<>();
        for (int i = 0; i < urls.size(); i++) {
            urlList.add(urls.get(i));

            /*if (i == urls.size() - 1) {
                urlStr = urlStr + urls.get(i);
            } else {
                urlStr = urlStr + urls.get(i) + ",";
            }*/
        }
        if (type == 0) {
            /*if (!TextUtils.isEmpty(viewModel.addGoodsEntity.get().getPictures())) {
                urlStr = viewModel.addGoodsEntity.get().getPictures() + "," + urlStr;
            }*/
            if (viewModel.addGoodsEntity.get().getPictureList() != null) {
                viewModel.addGoodsEntity.get().getPictureList().addAll(urlList);
                urlList.clear();
                urlList.addAll(viewModel.addGoodsEntity.get().getPictureList());
            }

        }
        viewModel.addGoodsEntity.get().setPictureList(urlList);
        if (TextUtils.isEmpty(id)) {
            viewModel.addGoodsModel();
        } else {
            viewModel.editGoodsModel();
        }
    }

    public void showSuccess() {
        //MToast.showToast(AddGoodsActivity.this, "发布成功!");
        successEntity = new SuccessEntity();
        successEntity.setTitle("提交成功");
        successEntity.setContent("您发布信息已提交成功，请前往商品页面查看");
        successEntity.setButtonText("确定");
        successEntity.setType(SuccessType.SUCC_GOODS);
        CommitSuccessActivity.start(this, successEntity);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    fileList.clear();
                    urls.clear();
                    for (LocalMedia localMedia : selectList) {
                        File file = new File(localMedia.getRealPath());
                        fileList.add(file);
                        urls.add(localMedia.getRealPath());
                    }
                    adapter.setDatas(selectList, urls);
                    adapter.setFileList(fileList);
                    break;
            }
        }
    }

    public void showUnitPop(List<UnitEntity> unitEntityList) {
        this.unitEntityList = unitEntityList;
        if (!TextUtils.isEmpty(id)) {
            for (int i = 0; i < unitEntityList.size(); i++) {
                if (unitEntityList.get(i).getName().equals(binding.tvUnit.getText())) {
                    unitPosition = i;
                    break;
                }
            }
        }
        showUnitPop();
    }

    private void showUnitPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(AddGoodsActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String content = unitEntityList.get(options1).getName();
                binding.tvUnit.setText(content);
                unitPosition = options1;
                viewModel.addGoodsEntity.get().setPriceUnitNo(unitEntityList.get(options1).getNo());
                viewModel.addGoodsEntity.get().setPriceUnitName(unitEntityList.get(options1).getName());
            }
        }).setDecorView(getWindow().getDecorView().findViewById(android.R.id.content)).build();
        businessOption.setPicker(unitEntityList);
        businessOption.setSelectOptions(unitPosition);
        businessOption.show();
    }

    public void showCityPop() {

        if (!TextUtils.isEmpty(id)) {
            for (int i = 0; i < addressEntityList.size(); i++) {
                if (addressEntityList.get(i).getId().equals(viewModel.addGoodsEntity.get().getProvinceNo())) {
                    cityPosition1 = i;
                    break;
                }
            }

            for (int i = 0; i < addressEntityList.get(cityPosition1).getChildren().size(); i++) {
                if (addressEntityList.get(cityPosition1).getChildren().get(i).getId().equals(viewModel.addGoodsEntity.get().getCityNo())) {
                    cityPosition2 = i;
                    break;
                }
            }

            for (int i = 0; i < addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().size(); i++) {
                if (addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(i).getId().equals(viewModel.addGoodsEntity.get().getDistrictNo())) {
                    cityPosition3 = i;
                    break;
                }
            }
        }

        //条件选择器
        CityPickView cityOption = new CityPickBuilder(AddGoodsActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String areaName;
                if (opt3.get(options1).get(options2).get(options3) == null) {
                    areaName = "";
                } else {
                    areaName = opt3.get(options1).get(options2).get(options3).getName();
                    cityPosition3 = options3;

                    viewModel.addGoodsEntity.get().setDistrictNo(opt3.get(options1).get(options2).get(options3).getNo());
                    viewModel.addGoodsEntity.get().setDistrictName(opt3.get(options1).get(options2).get(options3).getName());
                }
                String content = addressEntityList.get(options1).getName()
                        + opt2.get(options1).get(options2).getName()
                        + areaName;
                binding.tvStorage.setText(content);
                cityPosition1 = options1;
                cityPosition2 = options2;

                viewModel.addGoodsEntity.get().setCityNo(opt2.get(options1).get(options2).getNo());
                viewModel.addGoodsEntity.get().setCityName(opt2.get(options1).get(options2).getName());
                viewModel.addGoodsEntity.get().setProvinceNo(addressEntityList.get(options1).getNo());
                viewModel.addGoodsEntity.get().setProvinceName(addressEntityList.get(options1).getName());
            }
        }).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    public void showCategreyPop() {
        if (!TextUtils.isEmpty(id)) {
            for (int i = 0; i < categeryEntityList.size(); i++) {
                if (categeryEntityList.get(i).getId().equals(viewModel.addGoodsEntity.get().getFirstCategory())) {
                    categeryPosition1 = i;
                    break;
                }
            }

            for (int i = 0; i < categeryEntityList.get(categeryPosition1).getChildren().size(); i++) {
                if (categeryEntityList.get(categeryPosition1).getChildren().get(i).getId().equals(viewModel.addGoodsEntity.get().getSecondCategory())) {
                    categeryPosition2 = i;
                    break;
                }
            }

            for (int i = 0; i < categeryEntityList.get(categeryPosition1).getChildren().get(categeryPosition2).getChildren().size(); i++) {
                if (categeryEntityList.get(categeryPosition1).getChildren().get(categeryPosition2).getChildren().get(i).getId().equals(viewModel.addGoodsEntity.get().getThirdCategory())) {
                    categeryPosition3 = i;
                    break;
                }
            }

        }

        //条件选择器
        CityPickView categreyOption = new CityPickBuilder(AddGoodsActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String categery3Name;
                if (categery3.get(options1).get(options2).get(options3) == null) {
                    categery3Name = "";
                } else {
                    categery3Name = categery3.get(options1).get(options2).get(options3).getName();
                    categeryPosition3 = options3;
                    viewModel.addGoodsEntity.get().setThirdCategory(categery3.get(options1).get(options2).get(options3).getId());
                    viewModel.addGoodsEntity.get().setThirdCategoryName(categery3.get(options1).get(options2).get(options3).getName());
                }
                String content = categeryEntityList.get(options1).getName()
                        + "/" + categery2.get(options1).get(options2).getName()
                        + "/" + categery3Name;
                binding.tvCategory.setText(content);
                categeryPosition1 = options1;
                categeryPosition2 = options2;

                viewModel.addGoodsEntity.get().setFirstCategory(categeryEntityList.get(options1).getId());
                viewModel.addGoodsEntity.get().setFirstCategoryName(categeryEntityList.get(options1).getName());
                viewModel.addGoodsEntity.get().setSecondCategory(categery2.get(options1).get(options2).getId());
                viewModel.addGoodsEntity.get().setSecondCategoryName(categery2.get(options1).get(options2).getName());
            }
        }).build();
        categreyOption.setPicker(categeryEntityList, categery2, categery3);
        categreyOption.setSelectOptions(categeryPosition1, categeryPosition2, categeryPosition3);
        categreyOption.show();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // 子线程中解析省市区数据
                            initJsonData();
                        }
                    }).start();
                    break;

                case MSG_CATEGERY_LOAD_DATA:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            // 子线程中解析分类数据
                            initCategeryJsonData();
                        }
                    }).start();
                    break;

                case MSG_LOAD_SUCCESS:
                    showCityPop();
                    isLoaded = true;
                    break;

                case MSG_CATEGERY_LOAD_SUCCESS:
                    showCategreyPop();
                    isCategeryLoaded = true;
                    break;

                case MSG_LOAD_FAILED:
                    break;
            }
        }
    };

    private void initJsonData() {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> entity : addressEntityList) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
            opt2.add(entity.getChildren());
            for (int i = 0; i < entity.getChildren().size(); i++) {
                cityList.add(entity.getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (entity.getChildren().get(i).getChildren() == null
                        || entity.getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(entity.getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private void initCategeryJsonData() {
        for (NewCategeryEntity<List<NewCategeryEntity<List<NewCategeryEntity>>>> entity : categeryEntityList) {
            cg2 = new ArrayList<>();
            cg3 = new ArrayList<>();
            categery2.add(entity.getChildren());
            for (int i = 0; i < entity.getChildren().size(); i++) {
                cg2.add(entity.getChildren().get(i));
                List<NewCategeryEntity> categeryEntities = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (entity.getChildren().get(i).getChildren() == null
                        || entity.getChildren().get(i).getChildren().size() == 0) {
                    categeryEntities.add(null);
                } else {
                    categeryEntities.addAll(entity.getChildren().get(i).getChildren());
                }
                cg3.add(categeryEntities);
            }
            categery3.add(cg3);
        }
        mHandler.sendEmptyMessage(MSG_CATEGERY_LOAD_SUCCESS);
    }

    /*//分类数据
    public void showCategoryLeftView(List<MenuEntity<MenuEntity<MenuEntity>>> datas) {
        this.menuEntityList = datas;
    }*/

    //分类数据
    public void showNewCategoryLeftView(List<NewCategeryEntity<List<NewCategeryEntity<List<NewCategeryEntity>>>>> datas) {
        this.categeryEntityList = datas;
        if (isCategeryLoaded) {
            showCategreyPop();
        } else {
            mHandler.sendEmptyMessage(MSG_CATEGERY_LOAD_DATA);
        }
    }

    //地址数据
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
        if (isLoaded) {
            showCityPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }
    }

    /**
     * 保存编辑的dialog
     */
    private void showSaveDialog() {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("是否保存已编辑的内容！");

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //保存编辑状态
                viewModel.addGoodsEntity.get().setPrice(binding.etPrice.getText().toString());
                viewModel.addGoodsEntity.get().setBrand(binding.etBrand.getText().toString());
                viewModel.addGoodsEntity.get().setName(binding.etName.getText().toString());
                viewModel.addGoodsEntity.get().setRate(binding.etRate.getText().toString());

                String json = GsonManager.getInstance().getGson().toJson(viewModel.addGoodsEntity.get());
                SharedPreferenceUtil.getInstance().saveData("ADD_GOODS", json);
                roundCornerDialog.dismiss();
                finish();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
                SharedPreferenceUtil.getInstance().saveData("ADD_GOODS", "");
                finish();
            }
        });
    }
}
